/*
 * gs_button.c
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>

#include "gs_button.h"

typedef enum 
{
    GS_BST_NORMAL,
    GS_BST_HLIGHT,
    GS_BST_PUSHED,
} GS_BUTTONSTAT;

typedef struct _GS_BUTTONDATA
{
    PBITMAP normal;
	PBITMAP pushed;
    GS_BUTTONSTAT  stat;
} GS_BUTTONDATA;
typedef GS_BUTTONDATA* PGS_BUTTONDATA;

BITMAP normal;
BITMAP pushed;

static void draw_button (HWND hWnd, PGS_BUTTONDATA pData)
{
    HDC hdc;
    RECT rect;
    int oldmode;
    
	LOGFONT* logfont;
	SIZE size;
	int format;
	const char * caption = GetWindowCaption (hWnd);

    hdc = BeginPaint (hWnd);
    GetClientRect (hWnd, &rect);
    oldmode = SetBkMode (hdc, BM_TRANSPARENT);


	logfont = CreateLogFont (NULL, "song", "GB2312",
			FONT_WEIGHT_REGULAR, FONT_SLANT_ROMAN, FONT_SETWIDTH_NORMAL,
			FONT_SPACING_CHARCELL, FONT_UNDERLINE_NONE, FONT_STRUCKOUT_NONE,
			12, 0);
	SelectFont (hdc, logfont); 
	GetTextExtent (hdc, caption, -1, &size);

	if (pData->stat == GS_BST_NORMAL) {
		if (pData->normal)
			FillBoxWithBitmap (hdc, 0, 0, -1, -1, (PBITMAP) pData->normal);
	} 
	else {
		if (pData->pushed)
			FillBoxWithBitmap (hdc, 0, 0, -1, -1, (PBITMAP) pData->pushed);
	}

	format = DT_SINGLELINE | DT_VCENTER | DT_CENTER;
	DrawText (hdc, caption, -1, &rect, format);

	oldmode = SetBkMode (hdc, oldmode);

    EndPaint (hWnd, hdc);
}

static int GsButtonProc (HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    PGS_BUTTONDATA pData = NULL;
    
    if (message != MSG_CREATE)
        pData = (PGS_BUTTONDATA) GetWindowAdditionalData2 (hWnd);
            
    switch (message) 
    {
        case MSG_CREATE:
        {
            GS_BTNDATA *userdata;
            
            pData = (PGS_BUTTONDATA) calloc (1, sizeof (GS_BUTTONDATA));
            
            if (pData == NULL) return -1;

            //userdata = (GS_BTNDATA*) GetWindowAdditionalData(hWnd);
            userdata = NULL;
            
            if (userdata) {
                pData->normal = userdata->normal;
				pData->pushed = userdata->pushed;
            }
            else {
                pData->normal = &normal; // NULL;
				pData->pushed = &pushed; //NULL;
            }
            
            pData->stat = GS_BST_NORMAL;
            
            SetWindowAdditionalData2 (hWnd, (DWORD)pData);

            break; 
        }

        case MSG_DESTROY: 
            free ((void*) pData);
            break;

        case MSG_PAINT:
            draw_button (hWnd, pData);
            break;

        case MSG_MOUSEMOVEIN: 
        {
            BOOL in_out = (BOOL) wParam;
            
            if (in_out) 
                pData->stat = GS_BST_HLIGHT;
            else 
                pData->stat = GS_BST_NORMAL;
            InvalidateRect(hWnd, NULL, TRUE);
            
            break;
        }
        case MSG_LBUTTONDOWN: 
            
            pData->stat = GS_BST_PUSHED;
            InvalidateRect(hWnd, NULL, TRUE);
            
            break;

        case MSG_LBUTTONUP: 
            pData->stat = GS_BST_NORMAL;
            InvalidateRect(hWnd, NULL, TRUE);
            NotifyParentEx (hWnd, GetDlgCtrlID(hWnd), GS_BN_CLICKED, 0);
            break;
    }

    return DefaultControlProc (hWnd, message, wParam, lParam);
}

BOOL RegisterGsButton (void)
{
    WNDCLASS WndClass;

    WndClass.spClassName = GS_BUTTON;
    WndClass.dwStyle     = WS_NONE;
    WndClass.dwExStyle   = WS_EX_NONE;
    WndClass.hCursor     = GetSystemCursor (IDC_ARROW);
    WndClass.iBkColor    = GetWindowElementColor (BKC_CONTROL_DEF);
    WndClass.WinProc     = GsButtonProc;

	LoadBitmapFromFile(HDC_SCREEN, &normal, "./res/btn_normal.jpg");
	LoadBitmapFromFile(HDC_SCREEN, &pushed, "./res/btn_pushed.jpg");

    // printf("curr dir path = %s\n", get_current_dir_name());

    return RegisterWindowClass (&WndClass);
}

void UnregisterGsButton (void)
{
    UnregisterWindowClass (GS_BUTTON);
}


